

#include "flv_parser.h"
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <netinet/in.h>

FlvParser::FlvParser()
{

}

int FlvParser::ParserFlvFormatData(const uint8_t *data, int length, int &use_length, FlvFormat *flv_data)
{
	return 0;
}

int FlvParser::ParserFlvHeaderData(const uint8_t *data, int length, int &use_length, FlvHeader *flv_data)
{
	if (length < 9)
	{
		return 0;
	}
	int data_index = 0;
	uint8_t f_data = data[data_index++];
	uint8_t l_data = data[data_index++];
	uint8_t v_data = data[data_index++];
	if (f_data != (uint8_t)'F' && l_data != (uint8_t)'L' && v_data != (uint8_t)'V')
	{
		printf("check flv flag failed :%c:%c:%c.\n", f_data, l_data, v_data);
		return -1;
	}
	uint8_t version = data[data_index++];
	if (version != 0x01)
	{
		printf("flv header version : %d.\n", version);
		return -1;
	}
	uint8_t type_flag = data[data_index++];
	uint8_t has_audio = type_flag & 0x04;
	uint8_t has_video = type_flag & 0x01;
	uint32_t data_offset = 0;
	memcpy(&data_offset, data + data_index, sizeof(data_offset));
	data_offset = ntohl(data_offset);
	data_index += sizeof(data_offset);

	use_length = data_index;

	flv_data->InitMediaType(has_video, has_audio);
	flv_data->InitVersion(version);
	flv_data->InitDataOffset(data_offset);
	return 1;
}

int FlvParser::ParserFlvTagData(const uint8_t *data, int length, int &use_length, FlvTag &flv_data)
{
	if (length < 11)
	{
		return 0;
	}
	int data_index = 0;
	uint32_t previouse_tag_size = 0;
	memcpy(&previouse_tag_size, data, sizeof(previouse_tag_size));
	previouse_tag_size = ntohl(previouse_tag_size);
	data_index += sizeof(previouse_tag_size);
	uint8_t tag_type = data[data_index++];
	if (tag_type == 0x09)
	{
		
	}
	else if (tag_type == 0x08)
	{

	}
	else if (tag_type == 0x11)
	{

	}
	else
	{
		return -1;
	}
	return 0; 
}



